package com.tyut;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.tyut.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDateSource());
        String sql = "select count(*) from student";
        Long count = qr.query(sql, new ScalarHandler<Long>());
        System.out.println(count);
    }
    @Test
    public void C3p0DemoTest() throws PropertyVetoException, SQLException{
        //C3p0类的使用
        ComboPooledDataSource dataSource =new ComboPooledDataSource();

        /*
         * 使用四大参数组件 连接数据库
         */
        dataSource.setDriverClass("com.mysql.jdbc.Driver");

        dataSource.setJdbcUrl("jdbc:mysql://fantj.top:3306/stu_apt_sys");

        dataSource.setUser("root");

        dataSource.setPassword("root");

        //池配置
        dataSource.setInitialPoolSize(10);//初始化连接池时 初始化；连接数为10
        dataSource.setAcquireIncrement(5);  //设置连接池增量连接数
        dataSource.setMaxPoolSize(20);  //连接池最大连接数
        dataSource.setMinPoolSize(5);   //设置连接池最小连接数

        Connection connection =dataSource.getConnection();

        //使用的是动态代理机制
        System.out.println(connection);
        connection.close();
    }
}
